package com.example.telesys.controller;

import com.example.telesys.pojo.*;
import com.example.telesys.service.PhoneService;
import com.example.telesys.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/phone")
public class PhoneController {

    @Autowired
    private PhoneService phoneService;

    @Autowired
    private SupplierService supplierService;

    @RequestMapping("/selectAllPhones")
    public ApiResponse selectAllPhones(){
        List<Phone> phoneList = phoneService.selectAllPhones();
        for(Phone phone: phoneList){
            phone.setImageurl("../assets/image/phone/" + phone.getPhonename() + ".jpg");
        }
        return new ApiResponse(StatusCode.SUCCESS, StatusMessage.SUCCESS, phoneList);
    }

    @RequestMapping("/selectPhoneById")
    public ApiResponse selectPhoneById(@RequestParam("phoneid")String phoneid){
        Phone phone = phoneService.selectPhoneById(phoneid);
        return new ApiResponse(StatusCode.SUCCESS, StatusMessage.SUCCESS, phone);
    }

    @RequestMapping("/selectSupplierById")
    public ApiResponse selectSupplierById(@RequestParam("supplierid")String supplierid){
        Supplier supplier = supplierService.selectSupplierById(supplierid);
        return new ApiResponse(StatusCode.SUCCESS, StatusMessage.SUCCESS, supplier);
    }

}
